package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.annotations.Lang;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Renyuan;
import com.ruoyi.system.service.IRenyuanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * renyuanController
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/system/renyuan")
public class RenyuanController extends BaseController {
    private String prefix = "system/renyuan";

    @Autowired
    private IRenyuanService renyuanService;

    @RequiresPermissions("system:renyuan:view")
    @GetMapping()
    public String renyuan() {
        return prefix + "/renyuan";
    }

    /**
     * 查询renyuan列表
     */
    @RequiresPermissions("system:renyuan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Renyuan renyuan) {
        startPage();
        List<Renyuan> list = renyuanService.selectRenyuanList(renyuan);
        return getDataTable(list);
    }

    /**
     * 导出renyuan列表
     */
    @RequiresPermissions("system:renyuan:export")
    @Log(title = "renyuan", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Renyuan renyuan) {
        List<Renyuan> list = renyuanService.selectRenyuanList(renyuan);
        ExcelUtil<Renyuan> util = new ExcelUtil<Renyuan>(Renyuan.class);
        return util.exportExcel(list, "renyuan数据");
    }

    /**
     * 新增renyuan
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存renyuan
     */
    @RequiresPermissions("system:renyuan:add")
    @Log(title = "renyuan", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Renyuan renyuan) {
        return toAjax(renyuanService.insertRenyuan(renyuan));
    }

    /**
     * 修改renyuan
     */
    @GetMapping("/edit/{userid}")
    public String edit(@PathVariable("userid") String userid, ModelMap mmap) {
        Renyuan renyuan = renyuanService.selectRenyuanById(userid);
        mmap.put("renyuan", renyuan);
        return prefix + "/edit";
    }

    //@RequiresPermissions("system:renyuan:add")
    @PostMapping("/findone")
    @ResponseBody
    public Renyuan findone(Renyuan renyuan) {
        return renyuanService.selectRenyuanById(renyuan.getUserid());
    }

    /**
     * 修改保存renyuan
     */
    //@RequiresPermissions("system:renyuan:edit")
    @Log(title = "renyuan", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Renyuan renyuan) {
        return toAjax(renyuanService.updateRenyuan(renyuan));
    }

    /**
     * 删除renyuan
     */
    @RequiresPermissions("system:renyuan:remove")
    @Log(title = "renyuan", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(renyuanService.deleteRenyuanByIds(ids));
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String zhanghao, String mima, HttpServletRequest request) {
        zhanghao = zhanghao.trim();
        mima = mima.trim();
        Renyuan renyuan = renyuanService.selectRenyuanById(zhanghao);

        try {
            if (renyuan != null && renyuan.getPassword().equals(mima) ) {
                // 登录成功,记录session
                HttpSession session = request.getSession();
                session.setAttribute("userid", renyuan.getUserid());
                session.setAttribute("yijikeshi", renyuan.getYijikeshi());
                session.setAttribute("erjikeshi", renyuan.getYijikeshi());

                return success(renyuan.getUserid());
            }else{
                if(renyuan == null){
                    return error("用户不存在，请自行注册");
                }
                if(!renyuan.getPassword().equals(mima) ){
                    return error("用户密码错误");
                }
                return error("用户或密码错误");
            }

        } catch (AuthenticationException e) {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @PostMapping("/logout")
    @ResponseBody
    public AjaxResult ajaxLogout(HttpServletRequest request) {


        try {
            HttpSession session = request.getSession();
            session.removeAttribute("userid");
            session.removeAttribute("yijikeshi");
            session.removeAttribute("erjikeshi");
            return success();


        } catch (AuthenticationException e) {
            String msg = "退出失败";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }

    @PostMapping("/tbregister")
    @ResponseBody
    public AjaxResult tbregister(String zhanghao,
                                 String mima,
                                 String truename,
                                 String yijikeshi,
                                 String erjikeshi,
                                 String yaoqingma
                                 ) {
        try {
            zhanghao =zhanghao.trim();
            mima = mima.trim();
            truename = truename.trim();
            yaoqingma = yaoqingma.trim();
            if(!yaoqingma.equals("dzxxtb")){
                return error("邀请码错误");
            }
            if(zhanghao.equals("")||mima.equals("")||truename.equals("")){
                return error("证件号码、密码或姓名 不能为空");
            }
            // 通过身份证查询人员信息
            Renyuan renyuan = renyuanService.selectRenyuanById(zhanghao);
            if(renyuan!= null){
                if(renyuan.getFlag().equals("0")){
                    return error("该证件号码已注册，默认密码为身份证后六位");
                }else{
                    return error("该证件号码已注册!");
                }

            }
            Renyuan mmRenyuan = new Renyuan();
            mmRenyuan.setFlag(new Long(1));
            mmRenyuan.setChuangjiantime(new Date());
            mmRenyuan.setErjikeshi(erjikeshi);
            mmRenyuan.setLastedittime(new Date());
            mmRenyuan.setPassword(mima);
            mmRenyuan.setShenfenzhenghao(zhanghao);
            mmRenyuan.setStatus(new Long(1));
            mmRenyuan.setTruename(truename);
            mmRenyuan.setUserid(zhanghao);
            mmRenyuan.setYijikeshi(yijikeshi);

            int mmIsok = renyuanService.insertRenyuan(mmRenyuan);

            if (mmIsok == 1 ) {
                return success();
            }else{
                return error("注册账号失败");
            }

        } catch (AuthenticationException e) {
            String msg = "注册账号失败";
            if (StringUtils.isNotEmpty(e.getMessage())) {
                msg = e.getMessage();
            }
            return error(msg);
        }
    }



}
